package com.example.databindingdemo;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;

public class ImageViewBindingAdapter {
    /**
     *
     * @param imageView 该参数是传进来的UI控件
     * @param url 该参数是用来 传递网络图片地址的
     */
//    加载本地图片
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, String url){
        if (!TextUtils.isEmpty(url)){
            Glide.with(MyApplication.getContext()).load(url).into(imageView);
        }
    }
//    加载本地图片
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, int resid){
        imageView.setImageResource(resid);
    }
//    当网络图片为空显示本地图二选一
    @BindingAdapter(value = {"image","defaultImageResource"},requireAll = false)
    public static void setImage(ImageView imageView, String url,int resid){
        if (!TextUtils.isEmpty(url)){
            Glide.with(MyApplication.getContext()).load(url).into(imageView);
        }else {
            imageView.setImageResource(resid);
        }
    }
}
